package com.web.customer.controller;

import com.web.common.BaseResult;
import com.web.common.PendingAmend;
import com.web.common.enums.BaseStatus;
import com.web.common.enums.PendingAction;
import com.web.customer.entity.Customer;
import com.web.customer.service.CustomerService;
import com.web.pendingList.entity.PendingList;
import com.web.pendingList.service.PendingListService;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.logging.Logger;

/**
 * @author wenzhuo
 * @create 2024-01-09 2024/1/9
 */

@RestController
@CrossOrigin
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private PendingListService pendingService;

    public static Map<String,List<String>> optionCurrencyMap = new HashMap<String,List<String>>();

    Logger logger = Logger.getLogger(this.getClass().getName());


    @RequestMapping(value = "/customerList")
    public BaseResult<List<Customer>> getCustomerList(@ModelAttribute Customer customer){

        List<Customer> customerList = customerService.getCustomerList(customer.getCustomerNo(), customer.getCustomerGroup(),customer.getCustomerName());

        if(customerList.isEmpty()){
            return new BaseResult(BaseStatus.CUSTOMER_NOT_EXIST.getCode(),BaseStatus.CUSTOMER_NOT_EXIST.getDesc());
        }

        return new BaseResult(BaseStatus.SUCCESS.getCode(),customerList);
    }


    @PostMapping(value = "/addCustomer")
    public BaseResult addCustomer(@ModelAttribute Customer customer,String userName,String optionCurrency){

        //校验客户是否已存在
        if(customerService.QueryDuplicateData(customer.getCustomerGroup(),customer.getCustomerName()) != null){
            return new BaseResult<>(BaseStatus.CUSTOMER_ALREADY_EXIST.getCode(),BaseStatus.CUSTOMER_ALREADY_EXIST.getDesc());
        };

        //将需要生成账户的货币存起来
        List<String> currencyList= Arrays.asList(optionCurrency.split(","));
        optionCurrencyMap.put(customer.getCustomerName(),currencyList);
        logger.info("optionCurrencyMap: " + optionCurrencyMap.size());

        //新增Pending审批记录
        return pendingService.addCustomerPending(PendingAction.ADD.getAction(), Customer.MODULE, userName, customer);

    };

    @PostMapping(value = "/updateCustomer")
    public BaseResult updateCustomer(@ModelAttribute Customer customer,String userName){
        //根据CustomerNo查询出还未修改的Customer数据.
        List<Customer> customerList = customerService.getCustomerList(customer.getCustomerNo(), null, null);
        if(customerList.size() != 1){
            return new BaseResult<>(BaseStatus.CUSTOMER__DATA_EXCEPTION.getCode(),BaseStatus.CUSTOMER__DATA_EXCEPTION.getDesc());
        }
        Customer customerOld = customerList.get(0);

        //新增Customer审批记录,将修改的数据放入到临时表中.
        return pendingService.handleModifyCustomer(customerOld,customer,userName);
    };

    @PostMapping(value = "/deleteCustomer")
    public BaseResult deleteCustomer(@RequestParam("customerNo") Integer customerNo,String userName){
        //根据客户号判断客户是否存在
        List<Customer> customers = customerService.getCustomerList(customerNo, null,null);
        Customer customer;
        if(customers.size() == 1){
            customer = customers.get(0);
        }else{
            return new BaseResult<>(BaseStatus.CUSTOMER__DATA_EXCEPTION.getCode(),BaseStatus.CUSTOMER__DATA_EXCEPTION.getDesc());
        };
        //生成一笔Pending记录
       return pendingService.addCustomerPending(PendingAction.DEL.getAction(), Customer.MODULE, userName, customer);
    };

}
